export const calculateNextInterval = (card, quality) => {
  let easeFactor = card.easeFactor || 2.5
  let interval = card.interval || 0

  if (quality >= 3) {
    if (interval === 0) interval = 1
    else if (interval === 1) interval = 6
    else interval = Math.round(interval * easeFactor)
    
    easeFactor = easeFactor + (0.1 - (5 - quality) * (0.08 + (5 - quality) * 0.02))
  } else {
    interval = 0
    easeFactor = Math.max(1.3, easeFactor - 0.2)
  }

  easeFactor = Math.max(1.3, Math.min(2.5, easeFactor))
  return { interval, easeFactor }
}

export const getDueCards = (cards) => {
  return cards
    .filter(card => new Date(card.nextReview) <= new Date())
    .sort((a, b) => {
      const aInterval = a.interval || 0
      const bInterval = b.interval || 0
      return bInterval - aInterval
    })
}